package com.oe.rpc.spring;

import com.oe.rpc.common.ClassUtils;
import com.oe.rpc.common.EnumSerializeType;
import com.oe.rpc.exception.LdmRpcException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

public class LdmprotocolBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {


    @Override
    protected Class<?> getBeanClass(Element element) {
        return LdmProtocolBean.class;
    }

    @Override
    protected void doParse(Element element, BeanDefinitionBuilder builder) {

        String id = element.getAttribute("id");
        if(StringUtils.hasText(id)){
            element.setAttribute("id",id);
        }else {
            element.setAttribute("id","com.oe.rpc.spring.LdmProtocolBean");
        }
        String serialization = element.getAttribute("serialization");
        if(StringUtils.hasText(serialization)){
            int i = EnumSerializeType.matchCodeByName(serialization);
            if(i==-1){
                throw new LdmRpcException("can not find "+serialization +"Serialize Type!!!");
            }
            builder.addPropertyValue("serialization",i);
        }else {
            builder.addPropertyValue("serialization",0);
        }

        String port = element.getAttribute("port");
        if(StringUtils.hasText(port)){
            builder.addPropertyValue("port",port);
        }else {
            builder.addPropertyValue("port",8080);
        }


    }
}
